package com.report.controller;

import com.report.dto.Response;
import com.report.entity.Project;
import com.report.service.ProjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


/**
 * 项目信息、树形结构
 */
@RestController
@RequestMapping("/api/project")
public class ProjectController {

    @Autowired
    private ProjectService projectService;

    /**
     * 添加项目
     *
     * @param project
     * @return
     */
    @PostMapping
    public Response addProject(@RequestBody Project project) {
        Response response = new Response();
        response.setStatus("success");
        response.setData(projectService.saveProject(project));
        return response;
    }

    /**
     * 获取项目详情
     *
     * @param bsdw
     * @return
     */
    @GetMapping("/{bsdw}")
    public Response getProject(@PathVariable String bsdw) {
        Response response = new Response();
        response.setStatus("success");
        response.setData(projectService.getProject(bsdw));
        return response;
    }

    /**
     * 更新项目
     *
     * @param project
     * @return
     */
    @PutMapping
    public Response updateProject(@RequestBody Project project) {
        Response response = new Response();
        response.setStatus("success");
        response.setData(projectService.saveProject(project));
        return response;
    }

    /**
     * 删除项目
     *
     * @param sjxmbh
     * @return
     */
    @DeleteMapping("/{sjxmbh}")
    public Response deleteProject(@PathVariable String sjxmbh) {

        projectService.deleteProject(sjxmbh);
        Response response = new Response();
        response.setStatus("success");
        return response;
    }

    /**
     * 获取企业层级结构
     *
     * @param bsdwxydm
     * @return
     */
    @GetMapping("/tree/{bsdwxydm}")
    public Response getCompanyTree(@PathVariable String bsdwxydm) {
        Response response = new Response();
        response.setStatus("success");
        response.setData(projectService.buildCompanyTree(bsdwxydm));
        return response;
    }

    /**
     * 获取企业信用编号及名称
     *
     * @return
     */
    @GetMapping("/company")
    public Response getCompanyList() {
        Response response = new Response();
        response.setStatus("success");
        response.setData(projectService.findAll());
        return response;
    }
}
